package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"newck/cache"
	"newck/models"
)

type NewckController struct {
	beego.Controller
}

func (c *NewckController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	get, _ := cache.Rdb.Get(cache.Ctx, "list_redis").Result()
	var newck []*models.Newck
	if get == "" {
		models.DB.Find(&newck)
		jsonData, _ := json.Marshal(newck)
		cache.Rdb.Set(cache.Ctx, "list_redis", jsonData, 0)
	} else {
		json.Unmarshal([]byte(get), newck)
	}
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "查询成功",
		"data": newck,
	}
	c.ServeJSON()
	return
}
